package com.baizhi.controller;

import com.baizhi.common.PageDTO;
import com.baizhi.entity.Drug;
import com.baizhi.service.DrugService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("drug")
public class DrugController {
    private DrugService drugService;
    @Autowired
    public void setDrugService(DrugService drugService) {
        this.drugService = drugService;
    }
    @RequestMapping("showDrug")
    public String showDrug(PageDTO pd, Map map){
        PageInfo<Drug> pageInfo = drugService.selectDrugs(pd.getName(), pd.getPageNum(), pd.getPageSize());
        map.put("pageNum",pd.getPageNum());
        map.put("totalPage",pageInfo.getPages());
        map.put("list",pageInfo.getList());
        return "manager/drug/drugList";
    }
    @RequestMapping("addDrug")
    public String addDrug(Drug drug){
        drugService.insertDrug(drug);
        return "redirect:showDrug";
    }
    @RequestMapping("removeDrug")
    public String removeDrug(Integer drugid){
        drugService.deleteDrug(drugid);
        return "redirect:showDrug";
    }
    @RequestMapping("getOneDrug")
    public String getOneDrug(Integer drugid,Map map){
        Drug drug = drugService.selectById(drugid);
        map.put("drug",drug);
        return "manager/drug/updateDrug";
    }
    @RequestMapping("updateDrug")
    public String updateDrug(Drug drug){
        drugService.updateDrug(drug);
        return "redirect:showDrug";
    }
}
